<%= javascript_tag <<EOF
function finishPlayerAdd(home_away) {
  $('add_player_' + home_away).hide();
  $('link_player_' + home_away).show();
  Sortable.create(home_away + "_player_list", {constraint:false, containment:[home_away + '_player_list', home_away + '_squad'], dropOnEmpty:true, handle:'draggable', onChange:dropPlayerDetails, onUpdate:dropPlayerDetails, only:'player_squad', tag:'div'})
}

function ignoreEnter(event) {
  if (event.keyCode == Event.KEY_RETURN) return false;
  return true;
}

function addPlayerDetails(container) {
  attr = container.getElementsByClassName("player_attributes");
  for (i = 0; i < attr.size(); ++i) {
    children = attr[i].immediateDescendants();
    for (j = 0; j < children.size(); ++j) {
      children[j].enable();
    }
    attr[i].show();
  }
}

function dropPlayerDetails(container) {
  attr = container.getElementsByClassName("player_attributes");
  for (i = 0; i < attr.size(); ++i) {
    children = attr[i].immediateDescendants();
    for (j = 0; j < children.size(); ++j) {
      children[j].disable();
    }
    attr[i].hide();
  }
}
EOF
%>

<%= error_messages_for 'game' %>
<% content_for :main_title do %>
  <%= @game.phase.championship.full_name %>
<% end %>

<h2><%= @game.phase.name %></h2>

<% form_tag :action => :update_squad, :id => @game do %>
  <table class="game_show_score">
    <%= render :partial => "game_score" %>
    <tr>
      <td class="players">
        <div id="home_players">
          <h3>Players</h3>
          <div id="home_player_list">
            <%= render :partial => "player_squad", :collection => @home_players, :locals => { :disabled => true } %>
          </div>
          <%= render :partial => "insert_player",
            :locals => { :team => @game.home, :game => @game, :home_away => "home", :partial => "player_squad" } %>
        </div>
      </td>
      <td colspan="4" class="players">
        <h3>Players</h3>
        <div id="home_squad">
          <%= render :partial => "player_squad", :collection => @home_squad, :locals => { :disabled => false } %>
        </div>
      </td>
      <%= sortable_element "home_player_list", :dropOnEmpty => true, :containment => [ "home_player_list", "home_squad" ], :only => "player_squad", :constraint => false, :tag => "div", :onChange => "dropPlayerDetails", :onUpdate => "dropPlayerDetails", :handle => "draggable" %>
      <%= sortable_element "home_squad", :dropOnEmpty => true, :containment => [ "home_player_list", "home_squad" ], :only => "player_squad", :constraint => false, :tag => "div", :onChange => "dropPlayerDetails", :onUpdate => "addPlayerDetails", :handle => "draggable" %>
      <td></td>
      <td colspan="4" class="players">
        <h3>Players</h3>
        <div id="away_squad">
          <%= render :partial => "player_squad", :collection => @away_squad, :locals => { :disabled => false } %>
        </div>
      </td>
      </td>
      <td class="players">
        <div id="away_players">
          <h3>Players</h3>
          <div id="away_player_list">
            <%= render :partial => "player_squad", :collection => @away_players, :locals => { :disabled => true } %>
          </div>
          <%= render :partial => "insert_player",
            :locals => { :team => @game.away, :game => @game, :home_away => "away", :partial => "player_squad" } %>
        </div>
      </td>
      <%= sortable_element "away_player_list", :dropOnEmpty => true, :containment => [ "away_player_list", "away_squad" ], :only => "player_squad", :constraint => false, :tag => "div", :onChange => "dropPlayerDetails", :onUpdate => "dropPlayerDetails", :handle => "draggable" %>
      <%= sortable_element "away_squad", :dropOnEmpty => true, :containment => [ "away_player_list", "away_squad" ], :only => "player_squad", :constraint => false, :tag => "div", :onChange => "dropPlayerDetails", :onUpdate => "addPlayerDetails", :handle => "draggable" %>
    </tr>

    <tr><td colspan="10">
        <table>
          <tr><td>Round:</td><td><%= @game.round %></td></tr>
          <tr><td>Attendance:</td><td><%= @game.attendance %></td></tr>
          <tr><td>Date:</td><td><%= @game.formatted_date.to_s + " - " + @game.formatted_time.to_s %></td></tr>
          <tr><td>Stadium:</td><td><%= @game.stadium.name unless @game.stadium.nil? %></td></tr>
          <tr><td>Referee:</td><td><%= "#{@game.referee.name} (#{@game.referee.location})" unless @game.referee.nil? %></td></tr>
        </table>
    </td></tr>
  </table>

  <%= submit_tag "Update" %>
<% end %>
